﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class BaseData_Deal_PAYTYPE : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    string GroupCode = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200600102");
        if (Request["code"] != null)
        {
            GroupCode = Request["code"].ToString();
        }
        if (!IsPostBack)
        {
            BindDataLeft();
            BindDataRight();
        }
    }

    protected void BindDataLeft()
    {
        this.ListBoxLeft.DataSource = new BLL.PAYTYPE().GetAllList();
        this.ListBoxLeft.DataTextField = "PT_NAME";
        this.ListBoxLeft.DataValueField = "PT_CODE";
        this.ListBoxLeft.DataBind();
    }

    protected void BindDataRight()
    {
        this.ListBoxRight.DataSource = new BLL.PAYTYPE_DEALTYPE().P_DealType_GetPayTypeByCode(GroupCode).Tables[0].DefaultView;
        this.ListBoxRight.DataTextField = "PT_NAME";
        this.ListBoxRight.DataValueField = "PT_CODE";
        this.ListBoxRight.DataBind();
    }


    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        int Count = this.ListBoxLeft.Items.Count;
        for (int j = 0; j < Count; j++)
        {
            if (this.ListBoxLeft.Items[j].Selected == true)
            {
                ListItem _LiL = new ListItem();
                _LiL.Text = this.ListBoxLeft.Items[j].Text;
                _LiL.Value = this.ListBoxLeft.Items[j].Value;
                if (this.ListBoxRight.Items.Count == 0)
                {
                    this.ListBoxRight.Items.Add(_LiL);
                    this.ListBoxLeft.Items[j].Selected = false;
                }
                else
                {
                    for (int i = 0; i < this.ListBoxRight.Items.Count; i++)
                    {
                        ListItem _LiR = new ListItem();
                        _LiR.Text = this.ListBoxRight.Items[i].Text;
                        _LiR.Value = this.ListBoxRight.Items[i].Value;
                        if (_LiR.Value == _LiL.Value)
                        {
                            break;
                        }
                        if (_LiR.Value == this.ListBoxRight.Items[this.ListBoxRight.Items.Count - 1].Value)
                        {
                            this.ListBoxRight.Items.Add(_LiL);
                            //this.ListBoxLeft.Items.Remove(_LiL);
                            this.ListBoxLeft.Items[j].Selected = false;
                            break;
                        }
                    }
                }
            }
        }
    }
    protected void BtnRemove_Click(object sender, EventArgs e)
    {
        string Code = "";
        int Count = this.ListBoxRight.Items.Count;
        for (int i = 0; i < Count; i++)
        {
            if (this.ListBoxRight.Items[i].Selected == true)
            {
                ListItem _LiR = new ListItem();
                _LiR.Text = this.ListBoxRight.Items[i].Text;
                _LiR.Value = this.ListBoxRight.Items[i].Value;
                Code = _LiR.Value;
                this.ListBoxRight.Items.Remove(_LiR);
                //this.ListBoxLeft.Items.Add(_LiR);    
                Count = Count - 1;
                i = i - 1;
            }
            if (this.ListBoxRight.Items.Count == 1)
            {
                if (this.ListBoxRight.Items[0].Selected == true)
                {
                    this.ListBoxRight.Items.RemoveAt(0);
                    Count = Count - 1;
                    break;
                }
            }
        }
    }


    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("200600102", "00004"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        GroupCode = Request["code"].ToString();
        List<Model.PAYTYPE_DEALTYPE> uug = new List<Model.PAYTYPE_DEALTYPE>();
        foreach (ListItem s in ListBoxRight.Items)
        {
            Model.PAYTYPE_DEALTYPE u_ug = new Model.PAYTYPE_DEALTYPE();
            u_ug.PD_DEALTYPECODE = GroupCode;
            u_ug.PD_PAYTYPECODE = s.Value;
            uug.Add(u_ug);
        }
        try
        {
            new BLL.PAYTYPE_DEALTYPE().Delete(GroupCode);
            new BLL.PAYTYPE_DEALTYPE().Add(uug);
            Share.InsertLog("200600102", "00004");
            Share.Alert(this.Page, "操作成功");
        }
        catch
        {
            Share.Alert(this.Page, "操作失败");
        }
    }
}
